package com.hodge.core.config;

import com.hodge.core.filter.AuthHandler;
import com.hodge.core.filter.MyUsernamePasswordAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Collections;

/**
 * Created by 张皓竣 on 2020/9/19
 */
@Configuration
@RequiredArgsConstructor
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)//@Secured("admin")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Collections.singletonList("*"));
        configuration.setAllowedMethods(Collections.singletonList("*"));
        configuration.setAllowedHeaders(Collections.singletonList("*"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        AuthHandler authHandler = new AuthHandler();
        http
                .csrf().disable()// 取消跨站请求伪造防护
                .authorizeRequests()
                .antMatchers("/favicon.ico").permitAll()
                .antMatchers("/open/**").permitAll()
                .anyRequest().authenticated()   //其他的需要登陆后才能访问
                .and()
                .formLogin()
                //配置登录成功的自定义处理类
                .successHandler(authHandler)
                //配置登录失败的自定义处理类
                .failureHandler(authHandler)
                .and()
                //配置没有权限的自定义处理类
                .exceptionHandling()
                .authenticationEntryPoint(authHandler)
                .accessDeniedHandler(authHandler)
                // 开启 Basic 认证
                .and().httpBasic();

        http.logout()
                .logoutSuccessHandler(authHandler)
                .deleteCookies("JSESSIONID"); // 删除 JSESSIONID

        http.addFilterAt(new MyUsernamePasswordAuthenticationFilter(authenticationManager(), authHandler, authHandler),
                UsernamePasswordAuthenticationFilter.class);
    }
}
